package com.aletto.general_store.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.aletto.general_store.GeneralStoreApplication;
import com.aletto.general_store.R;
import com.aletto.general_store.data.entities.WalletAccount;
import com.aletto.general_store.data.model.Address;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class WalletAccountLocalStore {

    private static final String ADD_ACCOUNT = "ADD_ACCOUNT",
            ACCOUNTS                        = "ACCOUNTS";

    private static WalletAccountLocalStore sLocalConfigStore;

    public static WalletAccountLocalStore getInstance() {
        if (sLocalConfigStore == null) {
            synchronized (LocalConfigStore.class) {
                if (sLocalConfigStore == null) {
                    sLocalConfigStore = new WalletAccountLocalStore();
                }
            }
        }

        return sLocalConfigStore;
    }

    private final SharedPreferences mPreferences;

    public WalletAccountLocalStore() {
        mPreferences = GeneralStoreApplication.sInstance.getSharedPreferences("wallet_account_store", Context.MODE_PRIVATE);
    }

    public void updateAvatar(String address, String avatar){
        List<WalletAccount> accounts = accounts();

        for (WalletAccount account : accounts) {
            if (account.getAddress().equals(address)) {
                account.setAvatar(avatar);
                break;
            }
        }

        mPreferences.edit().putString(ACCOUNTS, new Gson().toJson(accounts)).apply();
    }

    public void updateName(String address, String nickname){
        List<WalletAccount> accounts = accounts();

        for (WalletAccount account : accounts) {
            if (account.getAddress().equals(address)) {
                account.setNickname(nickname);
                break;
            }
        }

        mPreferences.edit().putString(ACCOUNTS, new Gson().toJson(accounts)).apply();
    }

    public void addAccount(WalletAccount walletAccount) {
        List<WalletAccount> accounts = new ArrayList<>();
        String data = mPreferences.getString(ACCOUNTS, null);

        if (data != null) {
            accounts = new Gson().fromJson(data, new TypeToken<List<WalletAccount>>() {
                // ...
            }.getType());

            for (WalletAccount account : accounts) {
                if (account.getAddress().equals(walletAccount.getAddress())) {
                    ToastUtils.makeText(R.string.error_import_wallet);
                    return;
                }
            }

            accounts.add(walletAccount);

        } else { // 本地账户为空，直接存储

            accounts.add(walletAccount);
        }

        mPreferences.edit().putString(ACCOUNTS, new Gson().toJson(accounts)).apply();
    }

    public List<WalletAccount> accounts() {
        String data = mPreferences.getString(ACCOUNTS, null);
        if (data == null) {
            return new ArrayList<>();
        }

        return new Gson().fromJson(data, new TypeToken<List<WalletAccount>>() {

        }.getType());
    }

    public void removeAccount(String address) {
        String data = mPreferences.getString(ACCOUNTS, null);
        if (data != null) {
            List<WalletAccount> accounts = new Gson().fromJson(data, new TypeToken<List<WalletAccount>>() {
                // ...
            }.getType());

            for (int i = 0; i < accounts.size(); i++) {
                WalletAccount walletAccount = accounts.get(i);
                if (walletAccount.getAddress().equals(address)) {
                    accounts.remove(i);
                    break;
                }
            }

            mPreferences.edit().putString(ACCOUNTS, new Gson().toJson(accounts)).apply();
        }
    }

    public WalletAccount get(String address) {
        String data = mPreferences.getString(ACCOUNTS, null);
        if (data != null) {
            List<WalletAccount> accounts = new Gson().fromJson(data, new TypeToken<List<WalletAccount>>() {
                // ...
            }.getType());

            for (int i = 0; i < accounts.size(); i++) {
                WalletAccount walletAccount = accounts.get(i);
                if (walletAccount.getAddress().equals(address)) {
                    return walletAccount;
                }
            }
        }

        return null;
    }

}
